<template>
  <div class="merchant-dashboard">
    <!-- 数据概览卡片 -->
    <el-row :gutter="20" class="data-overview">
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>今日销售额</span>
              <el-tag size="small" type="success">实时</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="amount">¥{{ formatNumber(overview.todaySales) }}</div>
            <div class="trend" :class="{ 'up': overview.salesTrend > 0 }">
              {{ overview.salesTrend > 0 ? '+' : '' }}{{ overview.salesTrend }}%
              <el-icon><ArrowUp v-if="overview.salesTrend > 0" /><ArrowDown v-else /></el-icon>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>待处理订单</span>
              <el-tag size="small" type="warning">{{ overview.pendingOrders }}个</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="amount">{{ overview.pendingOrders }}</div>
            <div class="desc">需要处理的订单</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>商品总数</span>
              <el-tag size="small" type="info">{{ overview.totalgoods }}个</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="amount">{{ overview.totalgoods }}</div>
            <div class="desc">在售商品数量</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>售后处理</span>
              <el-tag size="small" type="danger">{{ overview.afterSales }}个</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="amount">{{ overview.afterSales }}</div>
            <div class="desc">待处理售后</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 快捷功能区 -->
    <el-row :gutter="20" class="quick-actions">
      <el-col :span="24">
        <el-card class="quick-actions-card">
          <template #header>
            <div class="card-header">
              <span>快捷功能</span>
            </div>
          </template>
          <el-row :gutter="20">
            <el-col :span="4" v-for="action in quickActions" :key="action.path">
              <el-button 
                class="quick-action-btn" 
                :type="action.type"
                @click="handleAction(action.path)"
              >
                <el-icon class="action-icon"><component :is="action.icon" /></el-icon>
                <span>{{ action.name }}</span>
              </el-button>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>

    <!-- 待处理事项 -->
    <el-row :gutter="20" class="pending-tasks">
      <el-col :span="12">
        <el-card class="task-card">
          <template #header>
            <div class="card-header">
              <span>待处理订单</span>
              <el-button type="primary" link @click="router.push('/merchant/orders')">
                查看全部
              </el-button>
            </div>
          </template>
          <el-table :data="pendingOrders" style="width: 100%" v-loading="loading">
            <el-table-column prop="orderNo" label="订单号" width="180"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
            <el-table-column prop="amount" label="金额">
              <template #default="scope">
                ¥{{ formatNumber(scope.row.amount) }}
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
              <template #default="scope">
                <el-button type="primary" link @click="viewOrder(scope.row.id)">
                  查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="task-card">
          <template #header>
            <div class="card-header">
              <span>待处理售后</span>
              <el-button type="primary" link @click="router.push('/merchant/after_sale')">
                查看全部
              </el-button>
            </div>
          </template>
          <el-table :data="afterSales" style="width: 100%" v-loading="loading">
            <el-table-column prop="orderNo" label="订单号" width="180"></el-table-column>
            <el-table-column prop="type" label="类型" width="100">
              <template #default="scope">
                <el-tag :type="getAfterSaleTypeTag(scope.row.type)">
                  {{ scope.row.typeText }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="申请时间"></el-table-column>
            <el-table-column label="操作" width="120">
              <template #default="scope">
                <el-button type="primary" link @click="viewAfterSale(scope.row.id)">
                  处理
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  Goods, 
  List, 
  Setting, 
  Ticket, 
  Service, 
  Plus,
  ArrowUp,
  ArrowDown
} from '@element-plus/icons-vue'
import service from '@/utils/request'

const router = useRouter()
const loading = ref(false)

// 数据概览
const overview = ref({
  todaySales: 0,
  salesTrend: 0,
  pendingOrders: 0,
  totalgoods: 0,
  afterSales: 0
})

// 快捷功能
const quickActions = [
  { name: '添加商品', icon: 'Plus', path: '/merchant/goods/add', type: 'primary' },
  { name: '商品管理', icon: 'Goods', path: '/merchant/goods', type: 'success' },
  { name: '订单管理', icon: 'List', path: '/merchant/orders', type: 'warning' },
  { name: '店铺设置', icon: 'Setting', path: '/merchant/settings', type: 'info' },
  { name: '优惠券', icon: 'Ticket', path: '/merchant/coupons', type: 'danger' },
  { name: '售后管理', icon: 'Service', path: '/merchant/after_sale', type: 'primary' }
]

// 待处理订单
const pendingOrders = ref([])

// 待处理售后
const afterSales = ref([])

// 获取仪表盘数据
const getDashboardData = async () => {
  loading.value = true
  try {
    const res = await service.get('/merchant/dashboard')
    if (res) {
      overview.value = res.overview
      pendingOrders.value = res.pendingOrders
      afterSales.value = res.afterSales
    }
  } catch (error) {
    console.error('获取仪表盘数据失败:', error)
  } finally {
    loading.value = false
  }
}

// 查看订单详情
const viewOrder = (orderId) => {
  router.push({
    name: 'MerchantOrderDetail',
    params: { id: orderId }
  })
}

// 查看售后详情
const viewAfterSale = (afterSaleId) => {
  router.push({
    name: 'MerchantAfterSaleDetail',
    params: { id: afterSaleId }
  })
}

// 格式化数字
const formatNumber = (num) => {
  return num.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

// 获取售后类型标签样式
const getAfterSaleTypeTag = (type) => {
  const typeMap = {
    'REFUND': 'danger',
    'EXCHANGE': 'warning',
    'RETURN': 'info'
  }
  return typeMap[type] || 'info'
}

// 处理快捷功能点击
const handleAction = (path) => {
  router.push({ path })
}

onMounted(() => {
  getDashboardData()
})
</script>

<style scoped>
.merchant-dashboard {
  padding: 20px;
}

.data-overview {
  margin-bottom: 20px;
}

.data-card {
  height: 160px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  text-align: center;
  padding: 10px 0;
}

.amount {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
}

.trend {
  font-size: 14px;
  color: #f56c6c;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.trend.up {
  color: #67c23a;
}

.desc {
  font-size: 14px;
  color: #909399;
}

.quick-actions {
  margin-bottom: 20px;
}

.quick-action-btn {
  width: 100%;
  height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.action-icon {
  font-size: 24px;
}

.pending-tasks {
  margin-bottom: 20px;
}

.task-card {
  margin-bottom: 20px;
}
</style> 